<template>
  <div class="recommend" ref="recommend">
    <scroll ref="scroll" class="recommend-content" :data="discList">
      <div>
        <div class="recommend-wrapper">
          <div v-if="banner.length" class="slider-wrapper" ref="sliderWrapper">
            <slider>
              <div v-for="(item,index) in banner" :key="index">
                <a>
                  <img class="needsclick" :src="item.picUrl" @load="loadImage">
                </a>
              </div>
            </slider>
          </div>
         </div>
        <div class="recommend-list">
            <h1 class="list-title">歌单推荐</h1>
            <ul class="list-body">
              <li v-for="(item, index) in discList" class="item" :key='index' @click="selectItem(item)">
                <img width="120" height="120" :src="item.coverImgUrl">
                <h2 class="name" v-html="item.name"></h2>
              </li>
            </ul>
        </div>
      </div>
      <div class="loading-container" v-show="!discList.length">
        <loading></loading>
      </div>
    </scroll>
    <router-view></router-view>
  </div>
</template>
<script type="text/ecmascript-6">
import {getRecommend, getDiscList} from 'api/recommend'
import Slider from 'base/slider/slider'
import Scroll from 'base/scroll/scroll'
import Loading from 'base/loading/loading'
import {playlistMixin} from 'common/js/mixin'
import {mapMutations} from 'vuex'
export default {
  mixins: [playlistMixin],
  data() {
    return {
      banner: [],
      discList: []
    }
  },
  components: {
    Scroll,
    Slider,
    Loading
  },
  created() {
    this._getRecomend()
    this._getDiscList()
  },
  methods: {
    ...mapMutations({
      setDisc: 'SET_DISC'
    }),
    // 点击跳转到相关歌单页面
    selectItem(item) {
      this.$router.push({
        path: `/recommend/${item.id}`
      })
      this.setDisc(item)
    },
    handlePlaylist(playlist) {
      const bottom = playlist.length > 0 ? '60px' : ''
      this.$refs.recommend.style.bottom = bottom
      this.$refs.scroll.refresh()
    },
    loadImage() {
      if (!this.checkloaded) {
        this.checkloaded = true
        this.$refs.scroll.refresh()
      }
    },
    _getRecomend() {
      getRecommend().then((res) => {
        if (res.code === 200) {
          this.banner = res.data
        }
      })
    },
    _getDiscList() {
      getDiscList().then((res) => {
        if (res.code === 200) {
          this.discList = res.data
        }
      })
    }
  }
}
</script>
<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"

  .recommend
    position: fixed
    width: 100%
    top: 88px
    bottom: 0
    .recommend-content
      height: 100%
      overflow: hidden
      .slider-wrapper
        position: relative
        width: 100%
        overflow: hidden
      .recommend-list
        .list-title
          height: 50px
          line-height: 50px
          text-align: left
          font-size: $font-size-medium-x
          color: #000
          margin-left:10px
        .list-body
          display:flex
          flex-flow:row wrap
          justify-content:space-around
          .item
            box-sizing: border-box
            margin-bottom: 10px
            width: 120px
            .name
              margin-bottom: 10px
              color: $color-text
              font-size: 5px
              line-height:15px
              margin-top: 5px
      .loading-container
        position: absolute
        width: 100%
        top: 50%
        transform: translateY(-50%)
</style>